//
//  OptionalView.m
//  Project two framework
//
//  Created by rimi on 16/7/23.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "OptionalView.h"
#import "OptionalCollectionViewCell.h"
#import "OpaionalCollectionViewCell1.h"
#import "ZPZNetRequest+ChooseByYourselfModeList.h"
#import "ZPZNetRequest.h"
#import "LStringUtils.h"
#import "AFNetworking.h"
#import "ZPZNetRequest+MealsList.h"
#import "DynamicView.h"
#import "ChooeseView.h"
#import "SingleView.h"
//#import "OptionalDetailsView.h"


@interface OptionalView ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
{
    UILabel *_titleLabel;
}

@property(nonatomic,strong)UIView                           *headView;

//
@property(nonatomic,strong)UIImageView                      *lietImage;
@property(nonatomic,strong)UIImageView                      *rightImage;

@property(nonatomic,strong)UITableView                      *tableView;
@property(nonatomic,strong)NSMutableArray                   *tableArray;
/**在tableView里面的集合视图*/
@property(nonatomic,strong)UICollectionView                 *collectionView;
@property(nonatomic,strong)NSMutableArray                   *collectionArray;
/**头部列表*/
@property(nonatomic,strong)UICollectionView                 *collectionView1;
@property(nonatomic,strong)NSMutableArray                   *collectionView1Array;
//创建另一个数组来解决cell重用的问题
@property(nonatomic,strong)NSMutableArray                   *collectionView2Array;
@property(nonatomic,assign)NSInteger                        number;

@property (nonatomic,strong)NSIndexPath                     *lastIndexPath;
@property(nonatomic,strong)OptionalCollectionViewCell       *collectionViewCell;
//@property(nonatomic,strong)DynamicView                      *dynamicView;
//@property(nonatomic,strong)OptionalDetailsView              *optionalDetailsView;
@property(nonatomic,strong)ChooeseView                      *chooeseView;


@property(nonatomic,strong)NSMutableArray                   *optionalDetailsViewArray;

@property(nonatomic,strong)UIScrollView                     *scrollView;

@end


@implementation OptionalView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(25, 80, 890, 500);
        [self headView];

        [self lietImage];
        [self rightImage];
        _tableArray = [[NSMutableArray alloc]init];
        _collectionArray = [[NSMutableArray alloc]init];
        [self tableView];
//        [self scrollView];
        [self collectionView];
        [self collectionView1];
        [self initializeDataSource];
        
        _collectionView1Array = [[NSMutableArray alloc]initWithArray:@[@"中银汇兑",@"境内外海外开户见证业务",@"借记卡产品",@"行用卡服务",@"理想之家.留学贷款",@"网上银行BOCNET",@"中银保险",@"中银理财产品"]];
    }
    _collectionView2Array = [[NSMutableArray alloc]init];
    for (int i = 0; i < _collectionView1Array.count; i++) {
        //创建一个字典，吧_collectionView1Array里面的东西装进这个字典里面
        NSDictionary *dic = @{@"title":_collectionView1Array[i],@"selet":@"NO"};
        [_collectionView2Array addObject:dic];
    }
    

    return self;
}

-(void)initializeDataSource{
    [ZPZNetRequest getByYourselfPageSize:@"30" PageNum:@"1" typeId:@"7" userId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            NSLog(@"%@",object);
            [_collectionArray addObjectsFromArray:object[@"content"]];
            [_collectionView reloadData];
        }
    }];

}

#pragma mark --按钮点击事件
//详情
-(void)clickDetailsBtn11:(UIButton *)sender{
    
    self.chooeseView = [ChooeseView showOtherView];
    self.chooeseView.bottomView.hidden = YES;
    NSString *string = [NSString stringWithFormat:@"%ld",self.number + 1];
    [ZPZNetRequest getByYourselfPageSize:@"30" PageNum:@"1" typeId:string userId:@"22" comletion:^(id object, BOOL success) {
        if (!success) {
            return ;
        }
        _chooeseView.nameLabel.text = object[@"content"][sender.tag][@"name"];
        _chooeseView.content.text = object[@"content"][sender.tag][@"info"];
        
    }];
    
}


//购买
-(void)clickChooseBtn:(UIButton *)sender{

    [SingleView showOtherView];
    
    
    
}

#pragma mark --cell点击事件

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_collectionView1 == collectionView) {
        //这里也是解决cell重用机制
        for (int i = 0; i<_collectionView2Array.count; i++) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:_collectionView2Array[i]];
            [dic setValue:@"NO" forKey:@"selet"];
            [_collectionView2Array replaceObjectAtIndex:i withObject:dic];
        }
        
        _titleLabel.hidden = YES;
        OpaionalCollectionViewCell1 *cell = (OpaionalCollectionViewCell1 *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.isSelet = @"YES";
        [_collectionView2Array replaceObjectAtIndex:indexPath.row withObject: @{@"title":cell.label10.text,@"selet":@"YES"}];
        [collectionView reloadData];
        self.number = indexPath.row;
        NSString *string = [NSString stringWithFormat:@"%ld",self.number + 1];
        [ZPZNetRequest getByYourselfPageSize:@"30" PageNum:@"1" typeId:string userId:@"22" comletion:^(id object, BOOL success) {
            if (success) {
                [_collectionArray removeAllObjects];
                [_collectionArray addObjectsFromArray:object[@"content"]];
                //判断如果_collectionArray.count == 0 把_collectionView隐藏掉
                if (_collectionArray.count == 0) {
                    _collectionView.hidden = YES;
                    UILabel *titleLabel = [[UILabel alloc]init];
                    titleLabel.frame = CGRectMake(self.bounds.size.width / 2 - 120, 320, 240, 40);
                    titleLabel.textColor = [UIColor whiteColor];
                    titleLabel.text = @"您要找的东西不见鸟！";
                    _titleLabel = titleLabel;
                    [self addSubview:titleLabel];
                }else{
                _collectionView.hidden = NO;
                    _titleLabel.hidden = YES;
//                    [_titleLabel removeFromSuperview];
//                    _titleLabel.alpha = 0;
                }
                [_collectionView reloadData];
            }
        }];
        
    }else if (_collectionView == collectionView) {
        
    }
}

#pragma mark --集合视图数据源
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _collectionView1) {
        //然后这里返回的不再是_collectionView1Array的长度
        return _collectionView2Array.count;
    }
    
    
    else{
    
        return _collectionArray.count;
    }
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == _collectionView1) {
        
        OpaionalCollectionViewCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
        //赋值也是在_collectionView2Array里面取值
        cell.label10.text = _collectionView2Array[indexPath.row][@"title"];
        //然后用isSelet来记录状态
        cell.isSelet = _collectionView2Array[indexPath.row][@"selet"];

        return cell;
    }else if(collectionView == _collectionView) {
    OptionalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OptionalCollectionViewCell" forIndexPath:indexPath];
        cell.headLabel.text = _collectionArray[indexPath.row][@"name"];
        
//        NSArray *array = _collectionArray[indexPath.row];
        cell.contentLabel.text = _collectionArray[indexPath.row][@"info"];
//        NSString *strStr = @"_collectionArray[indexPath.row]";

        
        
        //获取下标
        cell.detailsBtn.tag = indexPath.row;
        //详情按钮
        [cell.detailsBtn addTarget:self action:@selector(clickDetailsBtn11:) forControlEvents:UIControlEventTouchUpInside];
        //购买按钮
        [cell.chooseBtn addTarget:self action:@selector(clickChooseBtn:) forControlEvents:UIControlEventTouchUpInside];
        

       
    return cell;
    }
    else{
        return nil;
    }
}




#pragma mark --tableView数据源
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indexll = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indexll];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indexll];
    }
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%@",NSStringFromSelector(_cmd));
//}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 500;
}

-(UIView *)headView{
    if (_headView == nil) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(160, 20, 620, 20)];
        _headView.backgroundColor = [UIColor clearColor];
        [self addSubview:_headView];
    }

    return _headView;
}

-(UIImageView *)lietImage{
    if (_lietImage == nil) {
        _lietImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"箭头左.png"]];
        _lietImage.frame = CGRectMake(0, 0, 10, 20);
        [_headView addSubview:_lietImage];
    }
    return _lietImage;

}

-(UIImageView *)rightImage
{
    if (_rightImage == nil) {
        _rightImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed: @"箭头右.png"]];
        _rightImage.frame = CGRectMake( 610, 0, 10, 20);
        [_headView addSubview:_rightImage];
    }
    return _rightImage;

}

-(UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120, self.bounds.size.width, 500)];
        _tableView.rowHeight = 270;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //_tableView.scrollEnabled = YES;
        
        [self addSubview:_tableView];
    }
    return _tableView;
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(280, 250);
        flowLayout.minimumInteritemSpacing = 10;
//        flowLayout.minimumInteritemSpacing =
        
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 10, self.bounds.size.width, 260) collectionViewLayout:flowLayout];
        //可以上下滚动
//        _collectionView.alwaysBounceVertical = YES;
        //注册cell
        [_collectionView registerClass:[OptionalCollectionViewCell class] forCellWithReuseIdentifier:@"OptionalCollectionViewCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //_collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
        //不能滑动
//        _collectionView.scrollEnabled = NO;
        [_tableView addSubview:_collectionView];
    }

    return _collectionView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}

-(UICollectionView *)collectionView1
{
    if (_collectionView1 == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 0;
//        flowLayout.itemSize = CGSizeMake(self.bounds.size.width / 9, 20);
        flowLayout.itemSize = CGSizeMake(200, 20);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView1 = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 0, 600, 20) collectionViewLayout:flowLayout];
        [_collectionView1 registerClass:[OpaionalCollectionViewCell1 class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        _collectionView1.backgroundColor = [UIColor clearColor];
        _collectionView1.delegate = self;
        _collectionView1.dataSource = self;
        
        [_headView addSubview:_collectionView1];
    }
    return _collectionView1;

}

//-(UIScrollView *)scrollView
//{
//    if (_scrollView == nil) {
////         CGSize screesnSize = [UIScreen mainScreen].bounds.size;
//        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 120, self.bounds.size.width, 300)];
//        //里面的内容滚动的范围
//        _scrollView.contentSize = CGSizeMake(self.bounds.size.width, 310);
//        _scrollView.delegate = self;
//        _scrollView.backgroundColor = [UIColor whiteColor];
//        
////        _scrollView.pagingEnabled =
//        [self addSubview:_scrollView];
//        
//    }
//    return _scrollView;
//}

@end
